package com.tanger.common.utils;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.JsonNode;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.util.MultiValueMap;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.WebClient;

import java.time.Duration;

/**
 * @Description webClient连接工具类
 * @Author Hchenbin
 * @Date 2022/6/23 17:20
 * @Version 1.0
 */
public class WebClientUtil {

    //初始化
    private static WebClient webClient = WebClient.create();

    /**
     * get请求
     * @param url 请求路径
     * @return
     */
    public static JsonNode get(String url) {
        String body = webClient.get()
                .uri(url)
                .retrieve()
                .bodyToMono(String.class)
                .block(Duration.ofSeconds(5));
        JsonNode response = JSON.parse(body, JsonNode.class);
        System.err.println("Executive Outcomes:"+ body);
        return response;
    }

    /**
     * POST-JSON请求
     * @param url 请求地址
     * @param params JSON参数
     * @return
     */
    public static JsonNode postJson(String url, JSONObject params, String key, String value){
        String body = webClient.post()
                .uri(url)
                .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
                .header(HttpHeaders.ACCEPT,MediaType.ALL_VALUE)
                .header(key,value)
                .body(BodyInserters.fromValue(params.toJSONString()))
                .retrieve()
                .bodyToMono(String.class)
                .block(Duration.ofSeconds(5));
        JsonNode response = JSON.parse(body, JsonNode.class);
        System.err.println("Executive Outcomes:"+ body);
        return response;
    }

    /**
     * POST-表单请求
     * @param url 请求地址
     * @param params JSON参数
     * @return
     */
    public static JsonNode postForm(String url, MultiValueMap<String, Object> params, String key, String value){
        String body = webClient.post()
                .uri(url)
                .header(HttpHeaders.CONTENT_TYPE, MediaType.MULTIPART_FORM_DATA_VALUE)
                .header(HttpHeaders.ACCEPT,MediaType.ALL_VALUE)
                .header(key,value)
                .body(BodyInserters.fromValue(params))
                .retrieve()
                .bodyToMono(String.class)
                .block(Duration.ofSeconds(5));
        JsonNode response = JSON.parse(body, JsonNode.class);
        System.err.println("Executive Outcomes:"+ body);
        return response;
    }

    public static void main(String... args){
        String url1 = "http://8.141.173.16:10112/map-poi/CGtHiLn8yJJ/07550086/public/recommend/selected?recommendType=1";
        WebClientUtil.get(url1);

        String url = "http://8.141.173.16:10112/map-poi/CGtHiLn8yJJ/07550086/public/recommend/recommendPoi";
        JSONObject params = new JSONObject();
        params.put("floorName","L2");
        params.put("pageNo",1);
        params.put("pageSize",5);
        params.put("recommendId",3834);
        params.put("recommendType",1);
        WebClientUtil.postJson(url,params,"sign","");
    }

}
